﻿using Appliaction.Dto.Inv;
using Appliaction.IAppServices.Inv;
using AutoMapper;
using Domain.Entitys.Inv;
using Domain.IRepositorys.Inv;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Appliaction.AppServices.Inv
{
    public class Inv_othoutordServices : IInv_othoutordServices
    {

        private readonly IInv_othoutordRepository inv_OthoutordRepository;

        private readonly IMapper mapper;

        public Inv_othoutordServices(IInv_othoutordRepository inv_OthoutordRepository, IMapper mapper)
        {
            this.inv_OthoutordRepository = inv_OthoutordRepository;
            this.mapper = mapper;
        }
        /// <summary>
        /// 查询详情表
        /// </summary>
        /// <param name="Othoutord_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<List<Inv_othproddetails>> iInv_othproddetailslSelect(string Othoutord_code)
        {
            return inv_OthoutordRepository.iInv_othproddetailslSelect(Othoutord_code);
        }


        public List<othoutordSelectDto> inv_othoutordAsync(string searchKeyword, string Othoutord_type, int currentPage, int pageSize)
        {
            return inv_OthoutordRepository.inv_othoutordAsync(searchKeyword, Othoutord_type, currentPage, pageSize);
        }

        public Task<int> inv_othoutordCount(string searchKeyword, string Othoutord_type)
        {
            return inv_OthoutordRepository.inv_othoutordCount(searchKeyword, Othoutord_type);
        }
        /// <summary>
        /// 删除--其他出库单
        /// </summary>
        /// <param name="othoutord_id"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_othoutord_delete(int othoutord_id)
        {
            return inv_OthoutordRepository.Inv_othoutord_delete(othoutord_id);
        }
        /// <summary>
        /// 修改审核状态--其他出库单
        /// </summary>
        /// <param name="inv_Othoutord"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> Inv_othoutord_examineUpdate(Inv_othoutord_examineUpdateDto inv_Othoutord)
        {
            Inv_othoutord inv_Othoutord1 = mapper.Map<Inv_othoutord>(inv_Othoutord);
            return inv_OthoutordRepository.Inv_othoutord_examineUpdate(inv_Othoutord1);
        }


        /// <summary>
        /// 根据编号查询其他出库单信息
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        public Task<List<Inv_othoutord>> Inv_trf_SelectCode(string othoutord_code) {

            return inv_OthoutordRepository.Inv_trf_SelectCode(othoutord_code);
        }


        /// <summary>
        /// 根据编码查询其他出库单的详情信息
        /// </summary>
        /// <param name="trf_code"></param>
        /// <returns></returns>
        public Task<List<Inv_othproddetails>> Inv_trf_SelectDetail(string othoutord_code) {

            return inv_OthoutordRepository.Inv_trf_SelectDetail(othoutord_code);
        }
        /// <summary>
        /// 根据其他出库单的编码去查询Id来添加详情信息
        /// </summary>
        /// <param name="otgwr_code"></param>
        /// <returns></returns>
        /// <exception cref="NotImplementedException"></exception>
        public Task<int> othproddetailsSelect_id(string otgwr_code)
        {
            return inv_OthoutordRepository.othproddetailsSelect_id(otgwr_code);
        }

        /// <summary>
        /// 添加其他出库单信息
        /// </summary>
        /// <param name="inv_Othoutord"></param>
        /// <returns></returns>
        public Task<int> Inv_othoutordAdd(Inv_othoutordAddDto inv_Othoutord) {
            Inv_othoutord Inv_othoutord = mapper.Map<Inv_othoutord>(inv_Othoutord);
            return inv_OthoutordRepository.Inv_othoutordAdd(Inv_othoutord);
        }



        // <summary>
        /// 添加明细表的信息--其他出库单
        /// </summary>
        /// <param name="inv_Othwrproddtl"></param>
        /// <returns></returns>

        public async Task<int> Inv_othproddetails(List<Inv_othproddetailsAddDto> entities) {

            // 将DTO列表转换为实体列表
            var entitiesList = entities.Select(dto => new Inv_othproddetails
            {
                Othoutord_id = dto.Othoutord_id,
                Pro_name = dto.Pro_name,
                Pro_code = dto.Pro_code,
                pro_brand = dto.pro_brand,
                Pro_model = dto.Pro_model,
                unitmeas_id = dto.unitmeas_id,
                Othwrproddt_numder = dto.Othwrproddt_numder,
                Inventory_numder = dto.Inventory_numder,
                location_code = dto.location_code,
                location_name = dto.location_name,
                pro_cost_price = dto.pro_cost_price,
                pro_sale_price = dto.pro_sale_price,
                stock_entry_no = dto.stock_entry_no
            }).ToList();

            // 调用仓储层方法添加数据
            return await inv_OthoutordRepository.Inv_othproddetails(entitiesList);

        }


        /// <summary>
        /// 修改出库状态
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public Task<int> Inv_othproddetails_updateType(int id) { 
        
        return inv_OthoutordRepository.Inv_othproddetails_updateType(id);
        }

        /// <summary>
        /// 修改出库单和同一时间修改出库库单详情信息库单--详情出库单
        /// </summary>
        /// <param name="inv_Othoutord"></param>
        /// <returns></returns>
        public Task<int> Inv_othoutord_update(Inv_OthoutordUpdateMerage inv_OthoutordUpdateMerage) {
            Inv_othoutord inv_Othoutord=mapper.Map<Inv_othoutord>(inv_OthoutordUpdateMerage.inv_OthoutordUpdateDto);
            return inv_OthoutordRepository.Inv_othoutord_update(inv_Othoutord, inv_OthoutordUpdateMerage.inv_Othproddetails);
        }
    }
}